package com.example.dbsample.model.http;

import com.example.dbsample.model.enums.ResultCodeEnum;
import com.example.dbsample.util.json.JsonUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

public class LujsResponse<T> implements Serializable {
    private static final long serialVerssionUID = 1212131313L;

    private static final LujsResponse TIMEOUT = new LujsResponse("510", "timeout");
    private static final LujsResponse JSON_ERR = new LujsResponse("511", "");
    private String res_code;
    private String res_msg;
    private T data;

    public LujsResponse() {
    }

    public LujsResponse(ResultCodeEnum resultCodeEnum) {
        //this.res_code = resultCodeEnum.getCode();
        //this.res_msg = resultCodeEnum.getMsg();
        this(resultCodeEnum, (T) null);
    }

    public LujsResponse(ResultCodeEnum resultCodeEnum, T data) {
        this.res_code = resultCodeEnum.getCode();
        this.res_msg = resultCodeEnum.getMsg();
        this.data = data;
    }

    public LujsResponse(String res_code, String res_message) {
        this.res_code = res_code;
        this.res_msg = res_message;
    }

    public LujsResponse(String res_code, T data) {
        this.res_code = res_code;
        this.data = data;
    }

    public LujsResponse(String res_code, String res_msg, T data) {
        this.res_code = res_code;
        this.res_msg = res_msg;
        this.data = data;
    }

    public static LujsResponse timeout() {
        return TIMEOUT;
    }

    public static LujsResponse JSON_ERR() {
        return JSON_ERR;
    }

    public static LujsResponse fail(String message) {
        LujsResponse response = new LujsResponse("405", message);
        return response;
    }

    public static LujsResponse fail(String code, String message) {
        return new LujsResponse<>(code, message);
    }

    public static LujsResponse ofSuccess() {
        LujsResponse response = new LujsResponse("0", "success");
        return response;
    }

    public static <T> LujsResponse<T> ofSuccess(T data) {
        LujsResponse<T> response = new LujsResponse("0", "success");
        response.setData(data);
        return response;
    }

    public String getRes_code() {
        return this.res_code;
    }

    public void setRes_code(String res_code) {
        this.res_code = res_code;
    }

    public String getRes_msg() {
        return this.res_msg;
    }

    public void setRes_msg(String res_msg) {
        this.res_msg = res_msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @JsonIgnore
    public boolean isOK() {
        return "0".equals(this.res_code);
    }

    @JsonIgnore
    public boolean isTimeout() {
        return "9".equals(this.res_code);
    }

    public String toJson() {
        return JsonUtils.toJsonString(this);
    }
}
